<?php
/**
 * @title 2976. 转换字符串的最小成本 I
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }

class Solution {

    /**
     * @param String $source
     * @param String $target
     * @param String[] $original
     * @param String[] $changed
     * @param Integer[] $cost
     * @return Integer
     */
    function minimumCost($source, $target, $original, $changed, $cost) {
        /**
         * @since 2024-01-14 字符转变最低价格
         */
        $minCostChangedArray = [];

        /**
         * @since 2024-01-14 转换字符的数量
         */
        $changedCount = count($changed);
        for($i=0; $i<$changedCount; $i++){
            $minCostChangedArray[$original[$i]][$changed[$i]] = $cost[$i];
        }

        /**
         * @since 2024-01-14 起点字符
         */
        $sCharArray = array_keys($minCostChangedArray);

        /**
         * @since 2024-01-14 防止回溯，计算最小费用，不需要比较字符是否相同
         */
        $charString = "abcdefghijklmnopqrstuvwxyz";
        for($i=0; $i<strlen($charString); $i++){
            $minCostChangedArray[$charString[$i]][$charString[$i]] = 0;
        }

        /**
         * @since 2024-01-14 穷列所有转换
         */
        while($sCharArray){
            /**
             * @since 2024-01-14 pop一个元素
             */
            $s = array_pop($sCharArray);

            /**
             * @since 2024-01-05 BFS广度遍历
             */
            $nodeArray = $minCostChangedArray[$s];
            while($nodeArray){
                /**
                 * @since 2024-01-05 下次遍历元素的集合
                 */
                $nextNodeArray = [];

                /**
                 * @since 2024-01-05 遍历当前元素集合
                 */
                foreach ($nodeArray as $node=>$cost){
                    /**
                     * @since 2024-01-14 存在延生点，nextNode
                     */
                    if(isset($minCostChangedArray[$node])){
                        foreach ($minCostChangedArray[$node] as $nextNode=>$nextCost){
                            /**
                             * @since 2024-01-14 s到nextNode的费用
                             */
                            $sCost = $cost + $nextCost;

                            /**
                             * @since 2024-01-14 不存在就新增转换
                             */
                            if(!isset($minCostChangedArray[$s][$nextNode])){
                                $minCostChangedArray[$s][$nextNode] = $sCost;
                                $nextNodeArray[$nextNode] = $sCost;
                            } elseif($minCostChangedArray[$s][$nextNode] > $sCost){
                                /**
                                 * @since 2024-01-14 更少的费用
                                 */
                                $minCostChangedArray[$s][$nextNode] = $sCost;
                                $nextNodeArray[$nextNode] = $sCost;
                            } else {
                                /**
                                 * @since 2024-01-14 原费用更低，无需操作
                                 */
                            }
                        }
                    } else {}
                }

                /**
                 * @since 2024-01-05 下次遍历
                 */
                $nodeArray = $nextNodeArray;
            }
        }
        // return $minCostChangedArray;

        /**
         * @since 2024-01-14 字符转换最少费用
         */
        $minCost = 0;

        /**
         * @since 2024-01-14 遍历字符串
         */
        for($i=0; $i<strlen($source); $i++){
            /**
             * @since 2024-01-14 存在转换费用
             */
            if(isset($minCostChangedArray[$source[$i]][$target[$i]])){
                $minCost += $minCostChangedArray[$source[$i]][$target[$i]];
            } else {
                /**
                 * @since 2024-01-14 不能转换
                 * @return Integer
                 */
                echo $source[$i], ", ", $target[$i], PHP_EOL;
                return -1;
            }
        }

        /**
         * @return Integer
         */
        return $minCost;
    }
}


/**
 * @url http://127.0.0.1/leetcode/202401/2024.01.13_8.php
 */
$datas = [
    ["abcd", "acbe", ["a","b","c","c","e","d"], ["b","c","b","e","b","e"], [2,5,5,1,2,20]], // 28
    ["aaaa", "bbbb", ["a","c"], ["c","b"], [1,2]], // 12
    ["abcd", "abce", ["a"], ["e"], [10000]], // -1
    ["bcaabaddac", "bdccbdaadc", ["c","d","a","a","c","a","d"], ["a","a","d","b","d","c","c"], [4,3,6,3,11,6,4]], // 40
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    if(count($data) <= 20){
        var_dump($data);
    } else {
        var_dump(count($data));
    }

    $obj = new Solution();
    $result = $obj->minimumCost(...$data);
    // $result = $obj->($xhprof->tree($data));
    // $result = $obj->($xhprof->listNode($data));
    // $result = $obj->find(...$data);
    var_dump($result);
    // if(count($result)<=20){
    //     var_dump($result);
    // } else {
    //     var_dump(count($result));
    // }
    echo str_repeat(PHP_EOL, 3);
    echo str_repeat("<br>", 3);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $func = $data[0][$i];
//         $param = $data[1][$i];
//         $result = $obj->$func(...$param);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
